#include "main.h"
#include <stdlib.h>
#include <stdio.h>
#include <SDL/SDL_image.h>
#include <string.h>
#include <SDL/SDL.h>
#include "smpeg.h"
#include "perso.h"
    



    int LeJeu( )
   {
    unsigned int frameLimit = SDL_GetTicks() + 16;
    int continuer, maxW=1000, maxH =700,coll=0;
    int imgW=180;
    
    fd=serialport_init("/dev/ttyACM0", 9600);

    
        init("ESPRIT GAIDEN", &maxW, &maxH);

         initializePlayer();
    
          loadGame();
          


   
         atexit(cleanup);

    continuer = 1;


   

    while (continuer == 1)
    {
        


        
        
        getInput();


        if (jeu.level==1)
        {
        if(jeu.onMenu == 0)
        {
           
            updatePlayer(maxW, maxH);
            updateMonsters();
            updateButin();
        }
        else
        {
            if(jeu.menuType == START)
                updateStartMenu();
        }
       

       
        if(jeu.onMenu == 0)
        {
            
            draw(maxW, maxH);
        }
        else
        {
            if(jeu.menuType == START)
            {
                drawImage(map.background2, 0, 0); 
                drawStartMenu(maxW, maxH);
                SDL_Flip(jeu.ecran);
                SDL_Delay(1);
            }
               
        }



        
       }
       if (jeu.level==2)
       {    
           
            if(jeu.onMenu == 0)
        {
            
            updatePlayer2(maxW,maxH);

            drawmap2(maxW,maxH,imgW );
            if (Ninja.x - player.x< 400 && EnnemyDeath == 0)
            {
                animAura(&aura,maxW,maxH);
                player.x-=40;
                imgW-=20;
            }
            
            
            if (EnnemyDeath == 0)
            {


                attackAnimShuriken(&shuriken, coll,&player, maxW, maxH);
                
                coll=collide2(&shuriken,&player);
                if (coll==1)
                {
                    imgW-= 6;
                    if (imgW<0)
                    {
                        imgW=180;
                        initializePlayer();
                        jeu.vies-=1;
                    }

                }
            }

            
            SDL_Flip(jeu.ecran);
            }
        else
        {
            
                updateStartMenu();
                drawImage(map.background2, 0, 0); 
                drawStartMenu(maxW, maxH);
                SDL_Flip(jeu.ecran);
                SDL_Delay(1);
        }

       }

       if(jeu.level == 3)
       {
            if(jeu.onMenu == 0)
            {

                drawmap3(maxW,maxH);
                SDL_Flip(jeu.ecran);

            }
            else
            {
                updateStartMenu();
                drawImage(map.background2, 0, 0); 
                drawStartMenu(maxW, maxH);
                SDL_Flip(jeu.ecran);
                SDL_Delay(1);
            }
       }
       


        
        delay(frameLimit);
        frameLimit = SDL_GetTicks() + 16;

    }

    
    exit(0);

    }
